<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>柯里化函数</title>
</head>

<body>
    <script>

        function add(x, y, z) {
            return x + y + z
        }

        function curry(fn, arg) {
            arg = arg || []

            var len = fn.length

            return function () {
                // var subArg = [...arg, ...arguments]
                // var subArg = arg.concat(Array.from(arguments))
                var subArg = arg.concat([].slice.call(arguments))
                if (len === subArg.length) {
                    // return fn.call(this, ...subArg)
                    return fn.apply(this, subArg)
                } else {
                    return curry.call(this, fn, subArg)
                }
            }
        }

        var res = curry(add)
        console.log(add(5, 6, 2))
        console.log(res(5)(6)(2))


        // const curry2 = (fn) => {
           
        //     const _c = (rest_num, args_list) => {
        //         if (rest_num <= 0) {
        //             return fn(...args_list);
        //         }
        //         return (...args) => _c(rest_num - args.length, [...args_list, ...args])
        //     }
        //     return _c(fn.length, []);
        // }

        // // example
        // const plus = (a, b, c, d) => a + b + c + d;
        // const curry_plus = curry2(plus);
        // curry_plus(1, 2, 3, 4) // 10
        // curry_plus(1)(2, 3, 4) // 10
        // curry_plus(1)(2)(3, 4) // 10

    </script>
</body>

</html>